package org.example.page;

import lombok.extern.log4j.Log4j2;
import org.example.param.Parameter;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

@Log4j2
public class BatchDeletePanel extends JPanel implements ActionListener {
    private JPanel cardPanel;
    private CardLayout cardLayout;
    private JLabel deleteLabel;
    private JTextField deleteDirText;
    private JLabel selectLabel;
    private JButton selectBtn;
    private JButton enSureBtn;
    private String userHome;
    private JLabel preFileNameLabel;
    private JTextField preNameText;
    private File userSelectFile;
    private JButton returnBtn;


    public BatchDeletePanel(JPanel cardPanel, CardLayout cardLayout) {
        this.cardPanel = cardPanel;
        this.cardLayout = cardLayout;

        batchDeletePanelInit();

        btnInit();

    }

    private void btnInit() {

        selectLabel = new JLabel(Parameter.SELECT_DIR_LABEL);
        selectBtn = new JButton(Parameter.SELECT_DIR_BTN);
        deleteLabel = new JLabel(Parameter.DELETE_DIR_LABEL);
        deleteDirText = new JTextField();
        returnBtn = new JButton(Parameter.RETURN_BTN);
        enSureBtn = new JButton(Parameter.DELETE_ENSURE_BTN);
        preFileNameLabel = new JLabel(Parameter.FILE_NAME_PRE);
        preNameText = new JTextField();

        selectBtn.addActionListener(this);
        returnBtn.addActionListener(this);
        enSureBtn.addActionListener(this);


        this.add(selectLabel);
        this.add(selectBtn);
        this.add(deleteLabel);
        this.add(deleteDirText);
        this.add(preFileNameLabel);
        this.add(preNameText);

        this.add(returnBtn);
        this.add(enSureBtn);
    }

    private void batchDeletePanelInit() {
        //设置布局
        GridLayout gridLayout = new GridLayout(4, 2);
        this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        this.setLayout(gridLayout);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof  JButton){
            JButton btn = (JButton) e.getSource();
            switch (btn.getText()){
                case Parameter.SELECT_DIR_BTN: //选择打开目录
                    userSelectFile = openDirAndSelect();
                    if (userSelectFile == null){
                        break;
                    }
                    this.deleteDirText.setText(userSelectFile.getPath());
                    break;
                case Parameter.DELETE_ENSURE_BTN:
                    deleteFiles();
                    break;

                case Parameter.RETURN_BTN:
                    //返回
                    cardLayout.show(cardPanel,"mainPanel");
                    break;


            }


        }
    }

    private void deleteFiles() {
        if (!userSelectFile.exists() || !userSelectFile.isDirectory()){
            JOptionPane.showMessageDialog(null,"你选择的不是一个目录","警告",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (deleteDirText.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "目录不能为空", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (preNameText.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "前缀不能为空", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }
        //删除文件
        File[] files = userSelectFile.listFiles();
        String reg = "^" + preNameText.getText() + ".*";
        for (File file : files) {
            String fileName = file.getName();
            if (fileName.matches(reg)) {
//                log.info("测试正则通过");
                //删除文件
                if (file.delete()) {
                    log.info("删除" + fileName + "成功");
                } else {
                    log.error("删除" + fileName + "失败");
                }

            }
        }


    }

    private File openDirAndSelect() {
        if (userHome == null || userHome.isEmpty()){
            userHome = System.getProperty("user.home");
        }
        JFileChooser fileChooser = new JFileChooser(userHome);
        fileChooser.setDialogTitle("选择文件");
        //设置只能选择文件夹
//        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        //打开文件选择器
        int result = fileChooser.showDialog(this,"确认");

        //获取用户选择文件
        if (result == JFileChooser.APPROVE_OPTION){
            fileChooser.setVisible(false);
            return  fileChooser.getSelectedFile();
        }
        return null;
    }
}
